<template>
    <div v-loading="loading" class="app-container">
        <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="68px">
            <el-form-item label="课程" prop="courseId">
                <el-select v-model="queryParams.courseId" placeholder="请选择课程" clearable @change="handleChangeCoures">
                    <el-option v-for="item in courseSelectList" :key="item.courseId" :label="item.courseName"
                        :value="item.courseId" />
                </el-select>
            </el-form-item>
            <el-form-item label="班级" prop="classId">
                <el-select v-model="queryParams.classId" placeholder="请选择班级" clearable @focus="handleClassFocus">
                    <el-option v-for="item in classSelectList" :key="item.classId" :label="item.className"
                        :value="item.classId" />
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="Search" @click="gethhhList">搜索</el-button>
                <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>
        <div class="content">
            <div class="top-view">
                <div ref="llChartRef" class="ll-view"></div>
                <div ref="sjChartRef" class="sj-view"></div>
            </div>

            <div class="bottom-view">
                <div ref="b1ChartRef" class="sub-view"></div>
                <div ref="b2ChartRef" class="sub-view"> </div>
                <div ref="b3ChartRef" class="sub-view"> </div>
            </div>
        </div>

    </div>
</template>

<script setup name="analyse">
import * as echarts from 'echarts';
import { b1chart, b2Charts } from './b1Charts'
import { topChart } from './topChart'
import { onMounted } from 'vue';
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus';
import { queryCourseList, queryScoreRange } from '@/api/analyse.js'
import { queryClassByCourseId } from "@/api/register/registerList.js";

const router = useRouter()
const { proxy } = getCurrentInstance();
const courseSelectList = ref([])
const classSelectList = ref([]);
const queryParams = ref({
    courseId: null,
    classId: null
})
const llChartRef = ref()//饼图
const sjChartRef = ref()//饼图

const b1ChartRef = ref()//折线图
const b2ChartRef = ref()//折线图
const b3ChartRef = ref()//折线图

const loading = ref(false);

onMounted(() => {
    getSelectList()
})
// 柱状图
const topEchart = (data, ref, title) => {
    const myChart = echarts.init(ref);
    const option = topChart(data, title)
    option && myChart.setOption(option);
    // myChart.getZr()
    myChart.getZr().on('click', params => {

        let pointInPixel = [params.offsetX, params.offsetY]
        if (myChart.containPixel('grid', pointInPixel)) {
            let xIndex = myChart.convertFromPixel({ seriesIndex: 0 }, [params.offsetX, params.offsetY])[0]
            console.log("🚀 ~ myChart.getZr ~ params:", params, xIndex)
            handleGotoScore()
        }
    })
}

// 饼图
const b1Echart = (count, name) => {
    const myChart = echarts.init(b1ChartRef.value);
    const option = b2Charts(count, name, '分')
    console.log("🚀 ~ b1Echart ~ option:", option)
    option && myChart.setOption(option);
}
// 饼图
const b2Echart = (count) => {
    const myChart = echarts.init(b2ChartRef.value);
    const option = b2Charts(count, "及格率", '%')
    console.log("🚀 ~ b1Echart ~ option:", option)
    option && myChart.setOption(option);
}
// 饼图
const b31Echart = (count, name) => {
    const myChart = echarts.init(b3ChartRef.value);
    const option = b2Charts(count, name, '分')
    console.log("🚀 ~ b1Echart ~ option:", option)
    option && myChart.setOption(option);
}

/** 获取查询条件列表 */
function gethhhList() {
    loading.value = true;
    queryScoreRange(queryParams.value).then(response => {
        loading.value = false;
        const report = response.data.llList.map(i => i.scoreRangeCnt)
        const report1 = response.data.sjList.map(i => i.scoreRangeCnt)
        const score = response.data.scoreList[0]
        topEchart(report, llChartRef.value, '理论成绩分布')
        topEchart(report1, sjChartRef.value, '实践成绩分布')
        b1Echart(score.llScore, '理论平均分', '分')
        b2Echart(score.jgPre, "及格率", '%')
        b31Echart(score.sjScore, '实操平均分', '分')
    }).catch((error) => {
        console.log(error)
        loading.value = false;
    });

}
// change事件
const handleChangeCoures = (item) => {
    queryParams.value.classId = null
    classSelectList.value = []
    console.log("🚀 ~ handleChangeCoures ~ item:", item)
    queryClassByCourseId({ courseId: queryParams.value.courseId }).then(response => {
        if (response.code === 200) {
            classSelectList.value = response.data
        }
    });
}
const handleClassFocus = () => {
    if (!queryParams.value.courseId || queryParams.value.courseId === '') {
        ElMessage.warning('请选择课程')
        return
    }
}
/** 重置按钮操作 */
function resetQuery() {
    proxy.resetForm("queryRef");
    queryParams.value = {}
    gethhhList()
}
const getSelectList = async () => {
    const res1 = await queryCourseList()
    if (res1.code === 200) {
        courseSelectList.value = res1.rows
    }
    // const res2 = await examClasszList()
    // if (res1.code === 200) {
    //     classSelectList.value = res2.rows
    // }
    gethhhList()
}
const handleGotoScore = () => {
    router.push({
        path: '/scoreManager',
        query: queryParams.value

    })
}
</script>

<style lang="scss" scoped>
@import './index.scss'
</style>